package zhang.lao.service.console;/**
 * Created by zhangqin on 2017/9/28.
 */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zhang.lao.build.mybatis.jdbc.auto.model.*;
import zhang.lao.build.tool.ListUtils;
import zhang.lao.build.tool.UUIDTool;
import zhang.lao.dao.base.*;
import zhang.lao.service.console.plugin.WorkSplitModel;
import zhang.lao.service.console.plugin.WorkSplitTool;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author
 * @create 2017-09-28 14:52
 **/
@Service
public class WorkMapSplitService {

    @Autowired
    private WnMachineDeviceDao wnMachineDeviceDao;
    @Resource
    private WorkSplitTool workSplitTool;
    @Resource
    private WnMachineToolDao wnMachineToolDao;
    @Resource
    private WnMachineDeviceLinkDao wnMachineDeviceLinkDao;
    @Resource
    private WnMachineToolLinkDao wnMachineToolLinkDao;
    @Resource
    private WnMachineWorkReportGt06Dao wnMachineWorkReportGt06Dao;

    @Transactional
    public void initWork(String date){
        WnMachineDeviceExample wnMachineDeviceExample = new WnMachineDeviceExample();
        wnMachineDeviceExample.createCriteria().andDeviceTypeEqualTo("gt06");
        List<WnMachineDevice> wnMachineDeviceList = wnMachineDeviceDao.selectByExample(wnMachineDeviceExample);
        for (WnMachineDevice wnMachineDevice : wnMachineDeviceList) {
            int deviceId = wnMachineDevice.getId();
            String farmId = wnMachineDevice.getFarmId();
            String machineId = "";
            WnMachineDeviceLinkExample wnMachineDeviceLinkExample = new WnMachineDeviceLinkExample();
            wnMachineDeviceLinkExample.createCriteria().andDeviceIdEqualTo(deviceId);
            WnMachineDeviceLink first = ListUtils.getFirst(wnMachineDeviceLinkDao.selectByExample(wnMachineDeviceLinkExample));
            double fk = 1.00;
            if (first != null) {
                machineId = first.getMachineId();
                WnMachineToolLinkExample wnMachineToolLinkExample = new WnMachineToolLinkExample();
                wnMachineToolLinkExample.createCriteria().andMachineIdEqualTo(first.getMachineId());
                WnMachineToolLink first1 = ListUtils.getFirst(wnMachineToolLinkDao.selectByExample(wnMachineToolLinkExample));
                if (first1 != null) {
                    fk = wnMachineToolDao.selectByPrimaryKey(first1.getMachineToolId()).getBreadth();
                }
            }
            List<WorkSplitModel> list = workSplitTool.split(deviceId, date, fk);
            if (list != null && list.size() > 0) {
                WnMachineWorkReportGt06Example wnJobScheduleExample = new WnMachineWorkReportGt06Example();
                wnJobScheduleExample.createCriteria().andDeviceIdEqualTo(deviceId).andWorkDateEqualTo(date);
                if (wnMachineWorkReportGt06Dao.countByExample(wnJobScheduleExample) > 0) {
                } else {
                    for (WorkSplitModel workSplitModel : list) {
                        WnMachineWorkReportGt06 wnJobSchedule = new WnMachineWorkReportGt06();
                        wnJobSchedule.setDeviceId(deviceId);
                        wnJobSchedule.setFarmId(farmId);
                        wnJobSchedule.setMachineId(machineId);
                        wnJobSchedule.setEndTime(workSplitModel.getEndTime());
                        wnJobSchedule.setStartTime(workSplitModel.getStartTime());
                        wnJobSchedule.setId(UUIDTool.getUUID());
                        wnJobSchedule.setWorkBreadth(fk);
                        wnJobSchedule.setWorkArea(workSplitModel.getMj());
                        wnJobSchedule.setWorkDate(date);
                        wnMachineWorkReportGt06Dao.insert(wnJobSchedule);
                    }

                }

            }
        }
    }


}
